<template>
  <div class="rate" :class="{'disabled':disabled}">
    <span v-for="i in 5" :key="i" class="iconfont" @mouseenter="disabled?'':curScore=i" @mouseleave="disabled?'':curScore=''"  @click="disabled ? '' : setScore(i)" :class="getClass(i)">
      <i v-if="disabled && i==Math.floor(score)+1" class="iconfont" :style="'width:'+width"></i>
    </span>
    <!--<span v-if="showText" class="text">{{curScore||score}}分</span>-->
  </div>
</template>

<script>
export default {
  name: 'MyRate',
  props: {
    score: {
      type: Number,
      default: 0
    },
    disabled: {
      type: Boolean,
      default: false
    },
    showText: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      curScore: '',
      width: ''
    }
  },
  created: function () {
    this.getDecimal()
  },
  methods: {
    getClass (i) {
      if (this.curScore === '') {
        return i <= this.score ? 'icon-xingxing_xuanzhong' : 'icon-tubiaozhizuomoban'
      } else {
        return i <= this.curScore ? 'icon-xingxing_xuanzhong' : 'icon-tubiaozhizuomoban'
      }
    },
    getDecimal () {
      this.width = Number(this.score * 100 - Math.floor(this.score) * 100) + '%'
    },
    setScore (i) {
      this.$emit('update:score', i)
    }
  }
}
</script>
